package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.UsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.UsersForm;
import com.youlai.boot.factory.model.query.UsersQuery;
import com.youlai.boot.factory.model.vo.UsersVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 服装厂用户前端控制层
 *
 * @author youlaitech
 * @since 2025-07-14 19:43
 */
@Tag(name = "服装厂用户接口")
@RestController
@RequestMapping("/api/v1/wxUsers")
@RequiredArgsConstructor
public class wxUsersController {

    private final UsersService usersService;

    @Operation(summary = "服装厂用户分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:users:query')")
    public PageResult<UsersVO> getUsersPage(UsersQuery queryParams ) {
        IPage<UsersVO> result = usersService.getUsersPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增服装厂用户")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:users:add')")
    public Result<Void> saveUsers(@RequestBody @Valid UsersForm formData ) {
        boolean result = usersService.saveUsers(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取服装厂用户表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:users:edit')")
    public Result<UsersForm> getUsersForm(
        @Parameter(description = "服装厂用户ID") @PathVariable Long id
    ) {
        UsersForm formData = usersService.getUsersFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改服装厂用户")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:users:edit')")
    public Result<Void> updateUsers(
            @Parameter(description = "服装厂用户ID") @PathVariable Long id,
            @RequestBody @Validated UsersForm formData
    ) {
        boolean result = usersService.updateUsers(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除服装厂用户")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:users:delete')")
    public Result<Void> deleteUserss(
        @Parameter(description = "服装厂用户ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = usersService.deleteUserss(ids);
        return Result.judge(result);
    }
}
